﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AccessControl.Contracts.Settings;
using AccessControl.Contracts.Models;
using AccessControl.DataManagers;
using System.Collections.ObjectModel;
using AccessControl.Gateway.Managers;

namespace AccessControl.Gateway.Settings
{
	public class SensorsSettings : ISensorsSettings
	{
		#region Fields

		private static SensorsSettings s_Instance = null;
		ObservableCollection<ISensor> _Items = new ObservableCollection<ISensor>();

		#endregion

		#region Private Constructor

		private SensorsSettings()
		{
			DataManager.Instance.FillList(Items);
		}

		#endregion

		#region Public Properties

		public static SensorsSettings Instance
		{
			get 
			{
				lock (typeof(SensorsSettings))
				{
					if (s_Instance == null)
						s_Instance = new SensorsSettings();

					return s_Instance;
				}
			}
		}

		#endregion
		
		#region IDataManager<ISensor> Members

		public bool Add(ISensor item)
		{
			if (DataManager.Instance.Add(item))
			{
				/* Add to the entries manager */
				foreach (IEntry entry in EntriesManager.Instance.Items)
				{
					if (entry.Id == item.RelatedEntryId)
					{
						entry.Sensors.Add(item);
						break;
					}
				}

				/* And to the maps manager */
				foreach (IMap map in MapsManager.Instance.Items)
				{
					foreach (IMapEntry mapEntry in map.Entries)
					{
						if (mapEntry.Id == item.RelatedEntryId)
						{
							mapEntry.Sensors.Add(item);
							break;
						}
					}
				}

				return true;
			}

			return false;
		}

		public bool Update(ISensor item)
		{
			if (DataManager.Instance.Update(item))
			{
				/* Update in the entries manager */
				foreach (IEntry entry in EntriesManager.Instance.Items)
				{
					if (entry.Id == item.RelatedEntryId)
					{
						for (int i = 0; i < entry.Sensors.Count; ++i)
						{
							if (entry.Sensors[i].Id == item.Id)
							{
								entry.Sensors.RemoveAt(i);
								entry.Sensors.Insert(i, item);
								break;
							}
						}
					}
				}

				/* And in the maps manager */
				foreach (IMap map in MapsManager.Instance.Items)
				{
					foreach (IMapEntry mapEntry in map.Entries)
					{
						if (mapEntry.Id == item.RelatedEntryId)
						{
							for (int i = 0; i < mapEntry.Sensors.Count; ++i)
							{
								if (mapEntry.Sensors[i].Id == item.Id)
								{
									mapEntry.Sensors.RemoveAt(i);
									mapEntry.Sensors.Insert(i, item);
									break;
								}
							}
						}
					}
				}

				return true;
			}

			return false;
		}

		public bool Delete(ISensor item)
		{
			if (DataManager.Instance.Delete(item))
			{
				/* Update the entries manager */
				foreach (IEntry entry in EntriesManager.Instance.Items)
				{
					if (entry.Id == item.RelatedEntryId)
					{
						foreach (ISensor sensor in entry.Sensors)
						{
							if (sensor.Id == item.Id)
							{
								entry.Sensors.Remove(sensor);
								break;
							}
						}
					}
				}

				/* And the maps manager */
				foreach (IMap map in MapsManager.Instance.Items)
				{
					foreach (IMapEntry mapEntry in map.Entries)
					{
						if (mapEntry.Id == item.RelatedEntryId)
						{
							foreach (ISensor sensor in mapEntry.Sensors)
							{
								if (sensor.Id == item.Id)
								{
									mapEntry.Sensors.Remove(sensor);
									break;
								}
							}
						}
					}
				}

				return true;
			}

			return false;
		}

		#endregion

		#region IItemsContainer<ISensor> Members

		public IList<ISensor> Items
		{
			get { return _Items; }
		}

		public void Load()
		{
			Items.Clear();
			DataManager.Instance.FillList(Items);
		}

		#endregion
	}
}
